package com.zlcx.tz.live.common.config.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zlcx.tz.live.vo.ResultVO;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static com.zlcx.tz.live.constant.ResultStatus.NO_AUTH;

/**
 * auth filter中的权限拦截异常处理
 * @author anjl
 * @date 11:22 AM 2019/6/6
 * description:
 */
public class AuthExceptionEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws ServletException {
        response.setContentType("application/json");
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        ResultVO result = new ResultVO(NO_AUTH.getCode(), "无效的token！！！", request.getServletPath() + "  " + authException.getMessage());
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(response.getOutputStream(), result);
        } catch (Exception e) {
            throw new ServletException();
        }
    }
}
